package com.atguigu.tingshu.account.api;

import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.account.AccountDeductVo;
import com.atguigu.tingshu.model.account.UserAccount;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@Tag(name = "用户账户管理")
@RestController
@RequestMapping("api/account/userAccount")
@SuppressWarnings({"all"})
public class UserAccountApiController {

	@Autowired
	private UserAccountService userAccountService;




	@TsLogin
	@Operation(summary = "查看消费记录")
	@GetMapping("/findUserConsumePage/{pageNo}/{pageSize}")
	public Result findUserConsumePage(@PathVariable Long pageNo,
									  @PathVariable Long pageSize){
		Long userId = AuthContextHolder.getUserId();
		Page<UserAccountDetail> userAccountDetailPage = new Page<UserAccountDetail>(pageNo, pageSize);
		IPage<UserAccountDetail> iPage=userAccountService.findUserConsumePage(userAccountDetailPage,userId);

		return Result.ok(iPage);
	}

	@TsLogin
	@Operation(summary = "查看充值记录")
	@GetMapping("/findUserRechargePage/{pageNo}/{pageSize}")
	public Result findUserRechargePage(@PathVariable Long pageNo,
									  @PathVariable Long pageSize){
		Long userId = AuthContextHolder.getUserId();
		Page<UserAccountDetail> userAccountDetailPage = new Page<UserAccountDetail>(pageNo, pageSize);
		IPage<UserAccountDetail> iPage=userAccountService.findUserRechargePage(userAccountDetailPage,userId);

		return Result.ok(iPage);
	}



	@Operation(summary = "检查及扣减账户余额")
	@PostMapping("/checkAndDeduct")
	public Result checkAndDeduct(@RequestBody AccountDeductVo accountDeductVo){
		Boolean result = userAccountService.checkAndDeduct(accountDeductVo);


		return result ? Result.ok() : Result.fail();
	}

	@TsLogin
	@Operation(summary ="获取可用金额")
	@GetMapping("/getAvailableAmount")
	public Result getAvailableAmount() {
		Long userId = AuthContextHolder.getUserId();
		BigDecimal amount = userAccountService.getAvailableAmount(userId);
		return Result.ok(amount);
	}

}

